const { $Toast } = require('../../../utils/iview/base/index');
const Tools = require('../../../utils/modules/Tools');
import NetWork from '../../../utils/modules/NetWork';

const app = getApp();


Page({
  data: {
    auth: true,
    isloade: {
      hidden: false
    },
    _baseUrl: app.globalData._baseUrl,
    _imgPath: app.globalData._imgPath,
    _imgPaths: app.globalData._imgPaths,
    _backImg: app.globalData._backImg,
    img: [],
    user_type: 1,
    designer_id: '',
    openid: app.globalData.openid,
    shopName: '',
    shopId: '',
    userInfo: '',
    user: {},
    list: [{ time: '2019.08.19 18:00', money: '200.00' }, { time: '2019.08.19 18:00', money: '200.00' }]
  },
  onLoad(options) {
    var _that = this;
    var { type } = options;


    _that.setTitle('储值记录');
    var user = wx.getStorageSync('user');
    var userInfo = wx.getStorageSync('userInfo');
    var { user } = _that.data;
    _that.setData({
      user: user,
      userInfo: userInfo
    });

  },
  onShow: function () {
    var _that = this
    var user = wx.getStorageSync('user');
    var userInfo = wx.getStorageSync('userInfo');
    _that.record()

  },

  //储值记录
  record() {
    let _that = this;
    var token = wx.getStorageSync('token');
    if (!token) {
      return;
    }

    let net = new NetWork(_that.data._baseUrl)
    net._request({
      url: 'api/v1/member/recharge',
      data: {
        token: token,
      },
      method: 'POST'
    }, '', res => {

      _that.setData({
        order: res.data.data.data
      })
      console.log(res.data.data.data, "输出")
    }, res => {
      if (res.data.error_code == 400 && res.data.msg == 'token无效或已过期') {
        app.refreshtoken(token);
        setTimeout(() => {
          _that.record();
        }, 1500)
      }
    });
  },



  /* 拨打电话 */
  telPhone() {
  	var that = this;
  	wx.makePhoneCall({
  		phoneNumber: ''
  	}, res => {

  	});
  },
  /* 修改头部标题 */
  setTitle(str) {
    wx.setNavigationBarTitle({
      title: str,
      success: function () {

      }
    })
  },
  setType(e) {
    var index = e.target.dataset.index;
    this.setData({
      roomType: index
    })
  },
  onShareAppMessage(str) {
  	return {
  		title: str,
  		path: this.route
  	}
  },
  isUserInfo() {
    var _that = this;
    wx.getSetting({
      success(res) {
        // console.log(res)
        if (!res.authSetting['scope.userInfo']) {
          _that.setData({
            auth: false
          })

        }
      }
    })
  },

  userInfo(e) {

    let _that = this;
    let _userInfo = e.detail.userInfo;
    console.log(_userInfo)
    wx.setStorageSync('_userInfo', _userInfo);
    let net = new NetWork(app.globalData._baseUrl);
    let _data = e.detail;


    if (_userInfo) {
      this.setData({
        auth: true,
      })
    }


    wx.login({
      success(res) {
        if (res.code) {
          console.log(res.code, "code");

          net._request({
            url: 'api/v1/login/mini',
            method: 'POST',
            data: {
              code: res.code,
              rawData: _data.rawData,
              signature: _data.signature,
              iv: _data.iv,
              encryptedData: _data.encryptedData
            },
          }, '', res => {
            app.globalData.user = _userInfo;
            var user = res.data.data;
            wx.setStorageSync('userInfo', user);

            _that.setData({
              userInfo: user
            })

            wx.setStorageSync('user', {
              user_type: 1
            });
            $Toast({
              content: res.data.msg,
              type: 'success'
            });

          }, res => {
            console.log("失败返回", res);
            app.globalData.user = _userInfo;
            var user = res.data.data;
            wx.setStorageSync('userInfo', user);
            $Toast({
              content: res.data.msg,
              type: 'success'
            });
          });
        }
      }
    })
  }
})